/*******************************************************************************
*  file    : addmoledialog.cpp
*  created : 20.08.2013
*  author  : 
*******************************************************************************/

#include "addmoledialog.hpp"
#include "ui_addmoledialog.h"
#include <QPushButton>

#include <qlogger.hpp>
namespace{
QLogger* l = QLoggerEngine::getLogger("core");
}

AddMoleDialog::AddMoleDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddMoleDialog),
    phone_ok_(false),
    passw_ok_(false)
{
    ui->setupUi(this);
    phone_validator_ = new QRegularExpressionValidator(QRegularExpression("\\+380[0-9]{9}"),this);
    passw_validator_ = new QRegularExpressionValidator(QRegularExpression(("[\\d | \\w]{4,8}")),this);

    ui->phone->setValidator(phone_validator_);
    connect(ui->phone,  SIGNAL(textChanged(QString)), this, SLOT(onPhoneChange(QString)));
    connect(ui->passwd, SIGNAL(textChanged(QString)), this, SLOT(onPasswordChange(QString)));

    onPhoneChange   (ui->phone->text());
    onPasswordChange(ui->passwd->text());
    testOK();
}

AddMoleDialog::~AddMoleDialog()
{
    delete ui;
}

QString AddMoleDialog::phone() const
{
    return ui->phone->text();
}

QString AddMoleDialog::name() const
{
    return ui->name->text();
}

QString AddMoleDialog::passwd() const
{
    return ui->passwd->text();
}

QString AddMoleDialog::memo() const
{
    return ui->memo->document()->toPlainText();
}

QDateTime AddMoleDialog::bat_change() const
{
    return ui->dateEdit->dateTime();
}

void AddMoleDialog::setPhone(const QString & phone)
{
    ui->phone->setText(phone);
}

void AddMoleDialog::setName(const QString & name)
{
    ui->name->setText(name);
}

void AddMoleDialog::setPasswd(const QString & pass)
{
    ui->passwd->setText(pass);
}

void AddMoleDialog::setMemo(const QString & memo)
{
    ui->memo->setPlainText(memo);
}

void AddMoleDialog::setBatChange(const QDateTime & date)
{
    ui->dateEdit->setDateTime(date);
}

void AddMoleDialog::onPhoneChange(const QString &txt)
{
    QString str(txt);
    int pos = 0;
    QValidator::State st  = phone_validator_->validate(str,pos);
    switch(st)
        {
        case QValidator::Invalid :
            ui->phone->setStyleSheet("background-color: rgb(255, 0, 0);");
            phone_ok_ = false;
            testOK();
            break;
        case QValidator::Intermediate:
            ui->phone->setStyleSheet("background-color: rgb(255, 150, 0);");
            phone_ok_ = false;
            testOK();
            break;
        case QValidator::Acceptable:
            ui->phone->setStyleSheet("");
            phone_ok_ = true;
            testOK();
            break;
        }
}

void AddMoleDialog::onPasswordChange(const QString &txt)
{
    QString str(txt);
    int pos = 0;
    QValidator::State st  = passw_validator_->validate(str,pos);
    switch(st)
        {
        case QValidator::Invalid :
            ui->passwd->setStyleSheet("background-color: rgb(255, 0, 0);");
            passw_ok_ = false;
            testOK();
            break;
        case QValidator::Intermediate:
            ui->passwd->setStyleSheet("background-color: rgb(255, 150, 0);");
            passw_ok_ = false;
            testOK();
            break;
        case QValidator::Acceptable:
            ui->passwd->setStyleSheet("");
            passw_ok_ = true;
            testOK();
            break;
        }
}

void AddMoleDialog::testOK()
{
    if(ui->buttonBox->button(QDialogButtonBox::Ok))
        {
            if( passw_ok_ && phone_ok_)
                ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
            else
                ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
        }

}


